package com.xmy.service.impl;

import com.xmy.dao.BaseDao;
import com.xmy.entity.User;
import com.xmy.service.UserService;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserServiceImpl extends BaseDao implements UserService {
    @Override
    public int registUser(User user) {
        String sql = "INSERT INTO `xiaomibuydb`.`user`(`uid`, `uname`, `pwd`, `phone`, `nickname`, `signature`, `hobby`, `address`) VALUES (null,?,?,?,?,?,?,?)";
        return executeUpdate(sql,new Object[]{user.getUname(),user.getPwd(),user.getPhone()});
    }

    @Override
    public int selCountByName(String name) {
        String sql = "SELECT COUNT(1) FROM user WHERE uname = ?";
        int count = 0;
        try{
            ResultSet rs = executeQuery(sql,new Object[]{name});
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public User selByNameAndPwd(String uname, String pwd) {
        String sql = "SELECT * FROM user WHERE uname = ? and pwd = ?";
        User user= null;
        try{
            ResultSet rs = executeQuery(sql, new Object[]{uname, pwd});
            while (rs.next()){
                user = new User();
                user.setId(rs.getInt(1));
                user.setUname(rs.getString(2));
                user.setPwd(rs.getString(3));
                user.setPhone(rs.getString(4));
                user.setNickName(rs.getString(5));
                user.setSignature(rs.getString(6));
                user.setHobby(rs.getString(7));
                user.setAddress(rs.getString(8));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public int updateUserInfo(String column,String updateValue,int id) {
        String sql = "UPDATE `xiaomibuydb`.`user` SET `"+column+"` =? WHERE `uid` = ?";
        return executeUpdate(sql,new Object[]{updateValue,id});
    }

    @Override
    public User selUserInfoById(int id) {
        return null;
    }
}
